package org.xlx.xlx_oa.role_user.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xlx.xlx_oa.common.Constant;
import org.xlx.xlx_oa.role_user.convertor.RoleUserConvertor;
import org.xlx.xlx_oa.role_user.domain.*;
import org.xlx.xlx_oa.role_user.domain.dto.SysRoleDto;
import org.xlx.xlx_oa.role_user.domain.vo.SysRoleVo;
import org.xlx.xlx_oa.role_user.mapper.SysRoleControlRelMapper;
import org.xlx.xlx_oa.role_user.mapper.SysRoleMapper;
import org.xlx.xlx_oa.role_user.mapper.SysUserRoleRelMapper;
import org.xlx.xlx_oa.utils.ThreadLocalUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SysRoleService {
    private final RoleUserConvertor roleUserConvertor = RoleUserConvertor.INSTANCE;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleControlRelMapper controlRelMapper;
    @Autowired
    private SysUserRoleRelMapper userRelMapper;

    /**
     * 保存
     */
    public Boolean saveEntity(SysRoleVo roleVo) {
        SysRole sysRole = roleUserConvertor.convertVoToRole(roleVo);
        Long id = sysRole.getId();
        if (Objects.isNull(id)) {
            sysRoleMapper.insert(sysRole);
        } else {
            sysRoleMapper.updateById(sysRole);
        }
        if (roleVo.getControlVos() != null) {
            UpdateWrapper<SysRoleControlRel> wrapper = new UpdateWrapper<>();
            wrapper.lambda().eq(SysRoleControlRel::getRoleId, id);
            controlRelMapper.delete(wrapper);
            List<SysControl> sysControls = roleUserConvertor.convertVoToListControl(roleVo.getControlVos());
            for (SysControl sysControl : sysControls) {
                SysRoleControlRel rel = new SysRoleControlRel();
                rel.setRoleId(id);
                rel.setControlId(sysControl.getId());
                controlRelMapper.insert(rel);
            }
        }

        if (roleVo.getUserVos() != null) {
            UpdateWrapper<SysUserRoleRel> userWrapper = new UpdateWrapper<>();
            userWrapper.lambda().eq(SysUserRoleRel::getRoleId, id);
            userRelMapper.delete(userWrapper);
            List<AdmUser> users = roleUserConvertor.convertVoToListAdmUser(roleVo.getUserVos());
            for (AdmUser user : users) {
                SysUserRoleRel rel = new SysUserRoleRel();
                rel.setRoleId(id);
                rel.setUserId(user.getId());
                userRelMapper.insert(rel);
            }
        }
        return true;
    }

    /**
     * 删除
     */
    public Boolean removeEntity(Long id) {
        return sysRoleMapper.deleteById(id) > 0;
    }

    /**
     * 列表
     */
    public List<SysRoleDto> getList(SysRoleVo roleVo) {
        SysRole sysRole = roleUserConvertor.convertVoToRole(roleVo);
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(sysRole.getRoleName())) {
            wrapper.lambda().like(SysRole::getRoleName, sysRole.getRoleName());
        }
        Page<SysRole> page = new Page<>(roleVo.getPageNum(), roleVo.getPageSize());
        sysRoleMapper.selectPage(page, wrapper);
        return roleUserConvertor.convertToListSysRoleDto(page.getRecords());
    }

    /**
     * 详情
     */
    public SysRoleDto getDetail(Long id) {
        SysRole sysRole = sysRoleMapper.selectById(id);
        return roleUserConvertor.convertToSysRoleDto(sysRole);
    }

    /**
     * 是否具有增删改的能力
     */
    public Boolean isAdmin() {
        Object objUserId = ThreadLocalUtil.get(Constant.USERID);
        Long userId = Objects.isNull(objUserId) ? null : Long.valueOf(objUserId.toString());
        QueryWrapper<SysUserRoleRel> relQueryWrapper = new QueryWrapper<>();
        relQueryWrapper.lambda().eq(SysUserRoleRel::getUserId, userId);
        List<SysUserRoleRel> userRoleRels = userRelMapper.selectList(relQueryWrapper);
        ArrayList<Long> roles = new ArrayList<>();
        userRoleRels.forEach(rel -> roles.add(rel.getRoleId()));

        String roleIds = Constant.ROLE_IDS;
        String[] split = roleIds.split(",");
        ArrayList<Long> longs = new ArrayList<>();
        for (String s : split) {
            longs.add(Long.valueOf(s));
        }
        boolean flag = false;
        for (Long aLong : longs) {
            if (roles.contains(aLong)) {
                flag = true;
                break;
            }
        }

        return flag;
    }
}
